var prefix = jsctx + "/acctTenantOperator";

var acctRuleList = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 80;

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageAcctRule",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    // cach:false,
                    sidePagination: "server",
                    paginationShowPageGo: true,
                    height: h,
                    showColumns: false,
                    cache: false,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            operatorStationId: $('#operatorStationId').val(),
                            startTime: $('#startTime').val(),
                            endTime: $('#endTime').val()
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '', width: '50', align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'operatorName', title: '分销商名称', align: 'left', width: '100'},
                        {field: 'stationName', title: '站点', width: '100'},
                        {
                            field: '', title: '结算规则', width: '350', align: 'left',
                            formatter: function (value, row, index) {
                                if (row.elecAcctRule == null || row.elecAcctRule == '') {
                                    return '结算服务费=' + row.serviceAcctRule
                                } else if (row.serviceAcctRule == null || row.serviceAcctRule == '') {
                                    return '结算电费=' + row.elecAcctRule
                                } else {
                                    return '结算电费=' + row.elecAcctRule + ';结算服务费=' + row.serviceAcctRule
                                }
                            }
                        },
                        {field: 'remark', title: '结算说明', width: '200'},
                        {
                            field: '', title: '结算周期', width: '80', align: 'left',
                            formatter: function (value, row, index) {
                                if (row.halfMonthDay != null && row.halfMonthDay != '') {
                                    return '每月' + row.acctDay + '号/每月' + row.halfMonthDay + '号'
                                } else if (row.acctDay !='' && row.acctDay !=null){
                                    return '每月' + row.acctDay + '号'
                                } else {
                                    return '每日'
                                }

                            }
                        },
                        {
                            field: '', title: '规则有效时间', width: '180', align: 'left',
                            formatter: function (value, row, index) {
                                return row.ruleStartTime + '至' + row.ruleEndTime
                            }
                        },
                        {field: 'createdTime', title: '创建时间', width: '80'},
                        {field: 'createdBy', title: '创建人', width: '80'},
                        {field: 'statusString', title: '状态', width: '80'},
                        {
                            title: '操作',
                            field: 'roleId',
                            width: '200', align: 'center',
                            formatter: function (value, row, index) {
                                var a = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="编辑" onclick="acctRuleList.editAcctRule(\''
                                    + row.acctRuleId
                                    + '\')">编辑</a> ';

                                var b = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="停止" onclick="acctRuleList.updateAcctRuleStatus(\''
                                    + row.acctRuleId
                                    + '\')">停止</a> ';

                                var c = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="详情" onclick="acctRuleList.detailAcctRule(\''
                                    + row.acctRuleId
                                    + '\')">详情</a> ';

                                if (row.statusString == '进行中') {
                                    return b + c;
                                } else if (row.statusString == '未开始') {
                                    return a + c;
                                } else {
                                    return c;
                                }

                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function goBack() {
        location.href = prefix + "/index";
    }

    function editAcctRule(acctRuleId) {
        location.href = prefix + "/editAcctRule/" + acctRuleId;
    }

    function detailAcctRule(acctRuleId) {
        location.href = prefix + "/detailAcctRule/" + acctRuleId;
    }

    function addAcctRule() {
        var operatorStationId = $("#operatorStationId").val();
        if (operatorStationId == null) {
            alert('operatorStationId为空')
            return
        }
        location.href = prefix + "/addAcctRule/" + operatorStationId;
        // $.ajax({
        //     url: prefix + "/isExecuteAcctRule",
        //     type: "get",
        //     data: {
        //         operatorStationId: operatorStationId
        //     },
        //     success: function (data) {
        //         if (data.status == 200) {
        //             location.href = prefix + "/addAcctRule/" + operatorStationId;
        //         } else {
        //             layer.msg(data.message);
        //         }
        //     }
        // });
    }

    function updateAcctRuleStatus(acctRuleId) {
        layer.confirm('确定要停用选中的记录？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/updateAcctRuleStatus",
                type: "post",
                data: {
                    'acctRuleId': acctRuleId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("停用成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }


    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        editAcctRule: editAcctRule,
        updateAcctRuleStatus: updateAcctRuleStatus,
        addAcctRule: addAcctRule,
        detailAcctRule: detailAcctRule
    };
})();
